<?php
session_start();
include "connectdb.php";
require_once('VideoPlayer.class.php');

$username = $_SESSION['sess_Username'];
$password = $_SESSION['sess_Password'];
$emp_id = $_SESSION['sess_ID'];

$txtfile = $_POST["txtfile"];
$txtfile = str_replace("\\\\","/",$txtfile);
$b = explode("\\\\", $txtfile); 
$count = count($b);
$filename = $b[$count-1]; 		/////////////
$subfolder = $b[$count-2]; 		/////////////
$mainfolder = $b[$count-3]; 	/////////////

$vdoid = $_POST["txtvdoid"];
///////////////////
$vdo_series_name = $_POST["txtsrname"];
///////////////////
$vdoname = $_POST["txtname"];
$oldname = $_POST["oldname"];
$desc = $_POST["txtdesc"];
///////////////////
$vdo_author = $_POST["txtauthor"];
$vdo_year = $_POST["txtyear"];
$vdo_length = $_POST["txthour"].":".$_POST["txtmin"].":".$_POST["txtsec"];
///////////////////
$keyword = str_replace(" ","",trim($_POST["txtkeyword"]));
$view = $_POST["txtview"];
$vdo_votecount = $_POST["txtvotecount"];
$vdo_votesum = $_POST["txtvotesum"];
$today = Date("Y/m/d");

$fileold = $_POST["fileold"];
$sub_fileold = explode("/",$fileold);
$fileold2 = "$sub_fileold[0]"."\\"."$sub_fileold[1]"."\\"."$sub_fileold[2]";

$vdo_recommend = $cb_recommend;
if($cb_recommend == "on"){$vdo_recommend = "1";}
elseif($cb_recommend == ""){$vdo_recommend = "0";}

//////////////////////////////////////////////////////////////////////////
for ($i=0;$i<count($mediatype_array);$i++) {
$mediatypeid = $mediatype_array[$i];
}
//////////////////////////////////////////////////////////////////////////

if ($txtfile == $fileold){
		$use_path =  $fileold;
}else{
		$use_path =  $txtfile;
}

$_SESSION['video'] = $use_path;
$_SESSION['extension'] = NULL;
$_SESSION['accepted_extensions'] = array('wmv', 'asx','flv','mov','rmv','rmvb','swf', 'mpg', 'mpeg', 'mp4', 'avi');

    function verifyVideo() {
		  	$_SESSION['video'] = isset( $_SESSION['video'] ) ? $_SESSION['video'] : false;
		  	if( !$_SESSION['video'] || $_SESSION['video'] == '' ){
					return false;
			 } else {
					return true;
			 }
    }

    function verifyExtension() {
			$_SESSION['extension'] = NULL;
			$_SESSION['accepted_extensions'] = array('wmv', 'asx','flv','mov','rmv','rmvb','swf', 'mpg', 'mpeg', 'mp4', 'avi');
			$_SESSION['extension'] = end(explode('.', $_SESSION['video']));
		    if (!in_array($_SESSION['extension'], $_SESSION['accepted_extensions'])) {
					return false;
			} else {
					return true;
			}
    }

$SQL_SELECT_DUPNAME = "SELECT count(vdo_id) FROM ag_vdodetail where vdo_name = '$vdoname' ;";
$dupname = mysql_query($SQL_SELECT_DUPNAME);
$dupname2 = mysql_result($dupname,0);

				if( verifyVideo() == true && verifyExtension() == true ){
				
						$url = base64_encode("$txtfile");
						$SQL_UPDATE_VDO = "UPDATE ag_vdodetail SET 
															vdo_name='$vdoname',
															vdo_series_name='$vdo_series_name',
															vdo_desc='$desc',
															vdo_author='$vdo_author',
															vdo_year='$vdo_year',
															vdo_length='$vdo_length',
															vdo_url='$url',
															vdo_view='$view',
															vdo_votesum='$vdo_votesum',
															vdo_votecount='$vdo_votecount',
															media_typeid='$mediatypeid',
															vdo_keyword='$keyword',
															vdo_recommend='$vdo_recommend' 
															WHERE vdo_id=$id ;";
						$result = (mysql_db_query($dbname,$SQL_UPDATE_VDO) or die ("ไม่สามารถแก้ไขไฟล์นี้ได้"));
						
						$SQL_DELETE_TYPEDETAIL = "DELETE FROM ag_vdotypedetail WHERE vdo_id='$id' ;";
						$result_vdotypedetail = mysql_db_query($dbname,$SQL_DELETE_TYPEDETAIL) or die ("ไม่สามารถ clear vdotypedetail ได้");
					
						for ($i=0;$i<count($vdotype_array);$i++) 
						{
								$vdotypeid = $vdotype_array[$i];
								
								$SQL_INSERT_TYPEDETAIL = "INSERT INTO ag_vdotypedetail VALUES('$id','$vdotypeid') ;";
								$result_vdotypedetail = mysql_db_query($dbname,$SQL_INSERT_TYPEDETAIL) or die ("ไม่สามารถ insert vdotypedetail ได้");
						}
						?>
						<script>
								alert("แก้ไขรายการสื่อมัลติมีเดียเรียบร้อยแล้ว")
						</script>
						<!--<meta http-equiv='refresh' content='0;URL=vdo_edit.php?id=<?=$id?>'>-->
						<meta http-equiv='refresh' content='0;URL=vdo_list_edit.php'>
						<?
		
				}else	{
						?>
						<script>
								alert("ไม่พบไฟล์ที่ต้องการแก้ไข")
								history.back();
						</script>
						<?
				}

?>